﻿using System;
using System.ComponentModel;
using StaffWarsFm.Lib.Data;

namespace StaffWarsFm
{
    public class RadioViewModel : INotifyPropertyChanged
    {
        private bool isServer;
        private bool isWorking = false;
        private bool canChallenge;
        private User radioMaster;
        private User speakerMaster;
        private User currentUser;
        private User challengingUser;
        private Question question;

        public RadioViewModel()
        {
            IsEnabled = true;
        }

        public bool IsServer
        {
            get { return isServer; }
            set
            {
                isServer = value;
                OnPropertyChanged("IsServer");
            }
        }

        public bool IsWorking
        {
            get { return isWorking; }
            set
            {
                isWorking = value;
                IsEnabled = !isWorking;
                OnPropertyChanged("IsWorking");
                OnPropertyChanged("IsEnabled");
            }
        }

        public bool IsEnabled { get; private set; }

        public bool CanChallenge
        {
            get { return canChallenge; }
            set
            {
                canChallenge = value;
                OnPropertyChanged("CanChallenge");
            }
        }

        public User RadioMaster
        {
            get { return radioMaster; }
            set
            {
                radioMaster = value;
                OnPropertyChanged("RadioMaster");

                // You can only challenge other users
                CanChallenge = !RadioMaster.Equals(CurrentUser);
            }
        }

        public User SpeakerMaster
        {
            get { return speakerMaster; }
            set
            {
                speakerMaster = value;
                OnPropertyChanged("SpeakerMaster");
            }
        }

        public User ChallengingUser
        {
            get { return challengingUser; }
            set
            {
                challengingUser = value;
                OnPropertyChanged("ChallengingUser");
            }
        }

        public User CurrentUser
        {
            get { return currentUser;}
            set { currentUser = value; }
        }

        public Question Question
        {
            get { return question; }
            set
            {
                question = value;
                OnPropertyChanged("Question");
            }
        }

        protected virtual void OnPropertyChanged(string propertyName)
        {
            var args = new PropertyChangedEventArgs(propertyName);
            var handler = PropertyChanged;

            if (handler != null)
                handler(this, args);
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
